<template>
  <div class="tourism-info">
    <div class="complain-title-select">
      <label for="region-select" class="select-label">选择景区:</label>
      <div class="bgiselect">
        <select id="region-select" v-model="forewarnCrowded" @change="updateContent" class="custom-select">
          <option v-for="region in regions" :key="region.value" :value="region.value"
            class="custom-select large-font-select">{{ region.label }}</option>
        </select>
      </div>
    </div>
    <div class="details">
      <div class="row header">
        <span style="width: 10%; !important">序号</span>
        <span>投诉内容</span>
        <span>投诉信息</span>
        <span>投诉时间</span>
      </div>
      <div class="carousel">
        <div class="carousel-inner" :style="{ transform: `translateY(-${currentIndex * rowHeight}px)` }">
          <div class="row rowList" v-for="(item, index) in extendedData" :key="index"
            :class="{ 'alt-background': index % 2 !== 0 }">
            <span style="width: 10%; !important">{{ index }}</span>
            <span>{{ item.title }}</span>
            <span style="color: #FCC902;">{{ item.feel_type_text }}</span>
            <span>{{ item.date }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import dayjs from 'dayjs'

export default {
  data() {
    return {
      currentIndex: 1, // 初始化为1，以便直接滚动回第一条数据
      intervalId: null,
      rowHeight: 55, // 每行高度 (像素)
      forewarnCrowded: 4,
      extendedData: [],
      regions: [
        { label: '中国汉城', value: 1 },
        { label: '白竹园寺景区', value: 2 },
        { label: '石雕大观园', value: 3 },
        { label: '唐梓山景区', value: 4 },
        { label: '熊河风景区', value: 5 },
        { label: '无量台景区', value: 6 },
        { label: '白水寺景区', value: 7 },
      ],
    };
  },
  computed: {
    displayedData() {
      return this.extendedData;
    },
    extendedData() {
      if (this.displayedData.length > 0) {
        return [this.displayedData[this.displayedData.length - 1], ...this.displayedData, this.displayedData[0]];
      }
      return [];
    }
  },
  methods: {
    updateContent() {
      this.$axios.post('/api/forewarning/sceneryComplain', {
        scenery_id: this.forewarnCrowded
      })
        .then(res => {
          console.log('API Response:', res.data);
          if (res.data.code === 1) {
            this.extendedData = res.data.data;
          } else {
            console.error('Failed to fetch data:', res.data.message);
          }
        })
        .catch(error => {
          console.error('Failed to fetch data:', error);
        });
    },
    formatTimestamp(timestamp) {
      return dayjs(timestamp * 1000).format('YYYY-MM-DD HH:mm')
    },
    fetchData(tab) {
      this.$axios.post('/api/forewarning/sceneryComplain', {
        scenery_id: this.forewarnCrowded
      })
        .then(res => {
          console.log('API Response:', res.data);
          if (res.data.code === 1) {
            this.extendedData = res.data.data;
          } else {
            console.error('Failed to fetch data:', res.data.message);
          }
        })
        .catch(error => {
          console.error('Failed to fetch data:', error);
        });
    },
    resetCarousel() {
      clearInterval(this.intervalId);
      this.currentIndex = 1;
      this.startCarousel();
    },
    startCarousel() {
      if (this.extendedData.length > 6) {
        this.intervalId = setInterval(() => {
          this.currentIndex++;
          if (this.currentIndex >= this.extendedData.length - 7) {
            setTimeout(() => {
              this.currentIndex = 1;
            }, 500);
          }
        }, 2000);
      }
    }
  },
  mounted() {
    this.fetchData(this.extendedData);
    this.startCarousel();
  },
  beforeUnmount() {
    clearInterval(this.intervalId);
  }
};
</script>
<style scoped>
.tourism-info {
  width: 500px;
  height: 600px;
  color: #fff;
  padding-top: 10px;
}

.tabs {
  display: flex;
  font-size: 14px;
  margin-left: 10px;
}

.tab {
  width: 150px;
  height: 38px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('/public/imgs/no-choose.png');
  background-size: 100% auto;
  background-repeat: no-repeat;
}

.tab.active {
  background-image: url('/public/imgs/choose.png') !important;
}

.details {
  height: 100%;
}

.carousel {
  width: 100%;
  height: 383px;
  overflow: hidden;
  position: relative;
}

.carousel-inner {
  transition: transform 0.5s ease;
  font-size: 24px;
  background-color: rgba(29, 124, 221, 0.2);
}

.rowList {
  height: 55px;
  line-height: 1.5;
}

.row {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 55px;
  font-size: 12px;
}

.row span {
  display: inline-block;
  width: 25%;
  text-align: center;
}

.row.alt-background {
  background-color: rgba(67, 133, 204, 0.4);
}

.header {
  height: 41px;
  position: sticky;
  top: 0;
  background-color: rgba(3, 118, 177, 0.4);
}

.red {
  color: red;
}

.complain-title-select {
  outline: none;
  border: none;
  border-radius: 10px;
  padding: 5px;
  display: flex;
  flex-direction: row;
  align-items: center;
}

.select-label {
  padding-top: 3px;
  color: #fff;
  font-size: 14px;
}

.bgiselect {
  width: 135px;
  height: 31px;
  background-image: url('/public/imgs/no-choose.png');
  background-size: 100% 100%;
  background-position: top;
  border: none;
  outline: none;
  background-color: transparent;
  position: relative;
}

.custom-select {
  padding-left: 20px;
  width: 135px;
  height: 31px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  appearance: none;
  border: none;
  font-size: 15px;
  font-weight: bold;
  background-color: transparent;
  background-image: url('/public/imgs/KRRaN1s537.png');
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 12px 12px;
}

.large-font-select {
  font-size: 12px !important;
  color: #000 !important;

  option {
    font-size: 10px;
  }
}
</style>
